package com.shangxia.monomer.study.designPatterns.behavioralType.memento;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * @ProjectName: monomer
 * @ClassName: MementoDemo
 * @Time: 2021/8/7
 * @Author: zhouy
 * @Description:
 * @Experience:
 */

@Slf4j
public class MementoDemo {
    public static void main(String[] args) {
        Role role = new Role();
        role.initState();
        System.out.println("初始状态");
        role.showState();
        RoleStateMementoCaretaker caretaker = new RoleStateMementoCaretaker(role.saveState());
        System.out.println("存档");
        role.display();
        role.showState();
        System.out.println("die");
        role.recoveryState(caretaker.getRoleStateMemento());
        System.out.println("读档");
        role.showState();
    }
}

@Data
class Role {
    private String name;
    private Integer vitality;
    private Integer attack;
    private Integer defence;

    public void initState() {
        vitality = 100;
        attack = 100;
        defence = 100;
    }

    public RoleStateMemento saveState() {
        return new RoleStateMemento(vitality, attack, defence);
    }

    public void recoveryState(RoleStateMemento roleStateMemento) {
        this.vitality = roleStateMemento.getVitality();
        this.attack = roleStateMemento.getAttack();
        this.defence = roleStateMemento.getDefence();
    }

    public void showState() {
        System.out.println(vitality);
        System.out.println(attack);
        System.out.println(defence);
    }

    public void display() {
        vitality = 0;
        attack = 0;
        defence = 0;
    }
}

@Data
@AllArgsConstructor
class RoleStateMemento {
    private Integer vitality;
    private Integer attack;
    private Integer defence;
}

@Data
@AllArgsConstructor
class RoleStateMementoCaretaker {
    private RoleStateMemento roleStateMemento;
}